package com.soujiayi.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.soujiayi.adapter.TuanListAdapter;
import com.soujiayi.model.TuanCity;
import com.soujiayi.model.TuanMain;
import com.soujiayi.model.TuanMains;
import com.soujiayi.net.HttpRequester;
import com.soujiayi.net.NetworkUtils;
import com.soujiayi.store.DatabaseManager;
import com.soujiayi.store.SPResolver;
import com.soujiayi.util.Constants;

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TuanMainActivity extends Activity // extends FragmentActivity
		implements OnScrollListener {

	// private TextView tv_tuan_city;
	// private TextView tv_tab_other;
	// private TextView tv_tab_food;
	// private TextView tv_tab_fun;
	// private TextView tv_tab_makeup;
	// private TextView tv_tab_buy;
	// private TextView tv_tab_sport;
	// private ImageView iv_bottom_line;
	// private TextView tv_tuan_network;
	// private ViewPager vp_tuan_goods;
	// private final int CITY = 1;
	// private Animation animation;
	// private DatabaseManager databaseManager = new DatabaseManager(this);
	//
	// private float offset = 0;
	// private float bottomLineWidth;
	// private ArrayList<TuanViewPagerFragment> fragmentList;
	// private TuanFragmentPagerAdapter tfp_adapter;
	// private SPResolver spr;
	// private boolean cancelThread = false;

	// ///////////////////
	private String[] tuan_cat_array, tuan_price_array, tuan_order_array;
	private Spinner sp_tuan_cat, sp_tuan_price, sp_tuan_order;
	private TextView tv_tuan_city;
	private EditText et_tuan_search;
	private TextView tv_tuan_reload;
	private int catClick = 0;
	private int priceClick = 0;
	private int orderClick = 0;
	private int catId = 0;
	private int preCatId = 0;
	private String priceArea = "";
	private int orderBy = 0;
	private String searchKey = "";
	private SPResolver spr;
	private DatabaseManager databaseManager = new DatabaseManager(this);

	// //////
	private int pst = 0;
	private int maxSize = 0;
	private final int limit = 20;
	private LinearLayout ll_tuan_reloading;
	private TextView tv_tuan_total;
	private TextView tv_tuan_current;
	private View viewFooter;
	// private TextView tv_tuan_loadmore;
	private TextView tv_tuan_isLastOne;
	private TuanListAdapter tuan_list_adapter;
	private ListView lv_tuan_goods;
	private ArrayList<TuanMain> tuanList;
	private LinearLayout ll_tuan_loading;
	private LinearLayout ll_tuan_record;
	private HttpRequester httpRequester;
	private boolean isFirstIn = true;
	private boolean isLastPage = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.tuan_main);
		spr = new SPResolver(this, Constants.SP_SYSTEM, Context.MODE_PRIVATE);
		httpRequester = new HttpRequester(this);
		httpRequester.setHandler(handler);
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		registerReceiver(mNetworkStateReceiver, filter);
		initViews();
		initActionListener();
		initAdapter();
	}

	public void initViews() {
		tuan_cat_array = getResources().getStringArray(R.array.tuan_cat_array);
		tuan_price_array = getResources().getStringArray(
				R.array.tuan_price_array);
		tuan_order_array = getResources().getStringArray(
				R.array.tuan_order_array);

		sp_tuan_cat = (Spinner) findViewById(R.id.sp_tuan_cat);
		sp_tuan_price = (Spinner) findViewById(R.id.sp_tuan_price);
		sp_tuan_order = (Spinner) findViewById(R.id.sp_tuan_order);

		et_tuan_search = (EditText) findViewById(R.id.et_tuan_search);
		// mIconSearchClear =
		// this.getResources().getDrawable(R.drawable.search_clear);

		if (databaseManager.getCount(Constants.CITIES_TABLE_NAME) == 0) {
			ArrayList<TuanCity> tuanCityList = new ArrayList<TuanCity>();
			TuanCity city = new TuanCity();
			city.setCityid("3");
			city.setName(this.getString(R.string.tuan_city_default));
			city.setOrder("999");
			city.setFirstchar("S");
			city.setPinyin("shanghai");
			tuanCityList.add(city);
			databaseManager.insertCities(tuanCityList);
		}
		spr.OpenEditor();
		String cityId = spr.getValue(Constants.TUAN_CITYID, "3");
		spr.closeEditor();
		tv_tuan_city = (TextView) findViewById(R.id.tv_tuan_city);
		String cityName = databaseManager.getCityNameById(cityId);
		if (!"".equals(cityName))
			tv_tuan_city.setText(cityName);

		tv_tuan_city.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		ll_tuan_record = (LinearLayout) this.findViewById(R.id.ll_tuan_record);
		tv_tuan_total = (TextView) this.findViewById(R.id.tv_tuan_total);
		tv_tuan_current = (TextView) this.findViewById(R.id.tv_tuan_current);

		tv_tuan_total.setText(this.getString(R.string.tuan_tv_total).replace(
				"?", "0"));
		tv_tuan_current.setText(this.getString(R.string.tuan_tv_total).replace(
				"?", "0"));

		ll_tuan_reloading = (LinearLayout) this
				.findViewById(R.id.ll_tuan_reloading);
		lv_tuan_goods = (ListView) this.findViewById(R.id.lv_tuan_goods);
		viewFooter = this.getLayoutInflater().inflate(
				R.layout.tuan_list_footer, null);
		// tv_tuan_loadmore = (TextView) viewFooter
		// .findViewById(R.id.tv_tuan_loadmore);
		tv_tuan_isLastOne = (TextView) viewFooter
				.findViewById(R.id.tv_tuan_isLastOne);
		tv_tuan_reload = (TextView) viewFooter
				.findViewById(R.id.tv_tuan_reload);
		ll_tuan_loading = (LinearLayout) viewFooter
				.findViewById(R.id.ll_tuan_loading);

		lv_tuan_goods.addFooterView(viewFooter, null, false);
	}

	public void initAdapter() {

		ArrayAdapter<String> cat_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, tuan_cat_array);
		cat_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_tuan_cat.setAdapter(cat_adapter);

		ArrayAdapter<String> price_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, tuan_price_array);
		price_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_tuan_price.setAdapter(price_adapter);

		ArrayAdapter<String> order_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, tuan_order_array);
		order_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_tuan_order.setAdapter(order_adapter);
		tuanList = new ArrayList<TuanMain>();
		tuan_list_adapter = new TuanListAdapter(this, tuanList);
		reloadData();
	}

	public void initActionListener() {
		tv_tuan_city.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TuanMainActivity.this.startActivityForResult(new Intent(
						TuanMainActivity.this, TuanSelectCityActivity.class),
						Constants.TUAN_CITY_SELECTED);
				overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			}

		});
		sp_tuan_cat.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				catId = arg0.getSelectedItemPosition();
				if (catClick++ > 0) {
					reloadData();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		sp_tuan_price.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				int index = arg0.getSelectedItemPosition();

				switch (index) {
				case 0:
					priceArea = null;
					break;
				case 1:
					priceArea = "0_10";
					break;
				case 2:
					priceArea = "10_50";
					break;
				case 3:
					priceArea = "50_100";
					break;
				case 4:
					priceArea = "100_999999";
					break;
				}
				if (priceClick++ > 0) {
					reloadData();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		sp_tuan_order.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				orderBy = arg0.getSelectedItemPosition();

				if (orderClick++ > 0) {
					reloadData();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		lv_tuan_goods.setOnScrollListener(this);

		lv_tuan_goods.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int i,
					long arg3) {
				Intent intent = new Intent(TuanMainActivity.this,
						TuanDetailActivity.class);
				intent.putExtra(Constants.TUAN_MAIN, tuanList.get(i));
				intent.putExtra(Constants.TUAN_CITY, tv_tuan_city.getText()
						.toString());
				TuanMainActivity.this.startActivity(intent);
				TuanMainActivity.this.overridePendingTransition(
						R.anim.push_right_in, R.anim.push_right_out);
			}
		});
		et_tuan_search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				preCatId = sp_tuan_cat.getSelectedItemPosition();
				Intent intent = new Intent(TuanMainActivity.this,
						TuanSearchActivity.class);
				TuanMainActivity.this.startActivityForResult(intent,
						Constants.TUAN_SEARCH_SELECTED);
			}

		});

		// et_tuan_search.setOnTouchListener(new View.OnTouchListener() {
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// // TODO Auto-generated method stub
		// switch (event.getAction()) {
		// case MotionEvent.ACTION_UP:
		// int curX = (int) event.getX();
		// if (curX > v.getWidth() - 38
		// && !"".equals(et_tuan_search.getText().toString())) {
		// et_tuan_search.setText("");
		// int cacheInputType = et_tuan_search.getInputType();// backup the
		// input type
		// et_tuan_search.setInputType(InputType.TYPE_NULL);// disable soft
		// input
		// et_tuan_search.onTouchEvent(event);// call native handler
		// et_tuan_search.setInputType(cacheInputType);// restore input type
		// return true;// consume touch even
		// }
		// break;
		// }
		// return false;
		// }
		// });
		//

		// tv_tuan_loadmore.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// loadMoreData();
		// }
		//
		// });

		tv_tuan_reload.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isLastPage)
					reloadData();
				else
					loadMoreData();
			}

		});
	}

	public void reloadData() {
		ll_tuan_reloading.setVisibility(View.VISIBLE);
		lv_tuan_goods.setVisibility(View.GONE);
		// ll_tuan_record.setVisibility(View.GONE);

		tv_tuan_isLastOne.setVisibility(View.GONE);
		// tv_tuan_loadmore.setVisibility(View.GONE);
		ll_tuan_loading.setVisibility(View.GONE);

		isFirstIn = false;
		tuanList.clear();
		pst = 0;
		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msg = new Message();

				ArrayList<TuanMain> tuanMainlist = new ArrayList<TuanMain>();
				try {
					TuanMains tuanMains = new TuanMains();
					tuanMains = searchTuan(pst, limit);
					if (tuanMains != null) {
						if (tuanMains.getTotal_found() != null) {
							tuanMainlist = tuanMains.getTuanMainList();
							maxSize = Integer.valueOf(tuanMains
									.getTotal_found());
							pst = pst + limit;
						} else {
							maxSize = 0;
						}
					} else {
						msg.arg1 = -1;
						maxSize = 0;
					}
				} catch (Exception e) {
					msg.arg1 = -1;
					maxSize = 0;
					e.printStackTrace();
				}
				msg.obj = tuanMainlist;
				msg.what = Constants.TUAN_INIT_LOAD;
				handler.sendMessage(msg);
			}
		}).start();
	}

	public void loadMoreData() {
		// tv_tuan_loadmore.setVisibility(View.GONE);
		ll_tuan_loading.setVisibility(View.VISIBLE);
		tv_tuan_isLastOne.setVisibility(View.GONE);
		tv_tuan_reload.setVisibility(View.GONE);

		new Thread(new Runnable() {
			@Override
			public void run() {
				Message msg = new Message();
				ArrayList<TuanMain> tuanMainlist = new ArrayList<TuanMain>();
				try {
					TuanMains tuanMains = new TuanMains();
					tuanMains = searchTuan(pst, limit);
					if (tuanMains != null) {
						tuanMainlist = tuanMains.getTuanMainList();
						pst = pst + limit;
					} else
						msg.arg1 = -1;
				} catch (Exception e) {
					msg.arg1 = -1;
					e.printStackTrace();
				}
				msg.obj = tuanMainlist;
				msg.what = Constants.TUAN_LOAD_MORE;
				handler.sendMessage(msg);
			}
		}).start();
	}

	public TuanMains searchTuan(int index, int stepSize) {
		Map<String, String> parameters = new HashMap<String, String>();

		// parameters.put("istoday", "");
		String cityId = databaseManager.getCityIdByName(tv_tuan_city.getText()
				.toString());
		if (cityId == null || "".equals(cityId))
			cityId = "3";
		parameters.put("cityid", cityId);

		if (catId > 0)
			parameters.put("catid", String.valueOf(catId));

		if (searchKey != null && !"".equals(searchKey))
			parameters.put("q", searchKey);

		if (orderBy > 0)
			parameters.put("orderby", String.valueOf(orderBy));

		if (priceArea != null && !"".equals(priceArea))
			parameters.put("pricearea", priceArea);
		parameters.put("sid",
				((MyApplication) this.getApplication()).getSessionId());
		parameters.put("pst", String.valueOf(index));
		parameters.put("limit", String.valueOf(stepSize));
		return httpRequester.searchTuan(parameters);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (tuanList == null || tuanList.size() == 0)
			tv_tuan_current.setText(this.getString(R.string.tuan_tv_current)
					.replace("?", "0"));
		else
			tv_tuan_current.setText(this.getString(R.string.tuan_tv_current)
					.replace("?", String.valueOf((firstVisibleItem + 1))));

		if (totalItemCount >= maxSize + 1) {
			if (NetworkUtils.isNetworkConnected(this) && maxSize != 0)
				tv_tuan_isLastOne.setVisibility(View.VISIBLE);
			else
				tv_tuan_isLastOne.setVisibility(View.GONE);
			ll_tuan_loading.setVisibility(View.GONE);
			isLastPage = true;
		} else
			isLastPage = false;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			// 判断是否滚动到底部
			if (!NetworkUtils.isNetworkConnected(this)) {
				tv_tuan_isLastOne.setVisibility(View.GONE);
				ll_tuan_loading.setVisibility(View.GONE);
				tv_tuan_reload.setVisibility(View.VISIBLE);
				return;
			}
			if (view.getLastVisiblePosition() == view.getCount() - 1) {
				if (tuanList.size() < maxSize) {
					loadMoreData();
				}
			}

		}
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.arg1 == -1) {
				tv_tuan_reload.setVisibility(View.VISIBLE);
				ll_tuan_loading.setVisibility(View.GONE);
				tv_tuan_isLastOne.setVisibility(View.GONE);

				tv_tuan_total.setText(TuanMainActivity.this.getString(
						R.string.tuan_tv_total).replace("?",
						String.valueOf(maxSize)));
				if (msg.what == Constants.TUAN_INIT_LOAD)
					tv_tuan_current.setText(TuanMainActivity.this.getString(
							R.string.tuan_tv_current).replace("?", "0"));

				Toast.makeText(
						TuanMainActivity.this,
						TuanMainActivity.this
								.getString(R.string.load_data_failure),
						Toast.LENGTH_SHORT).show();
				lv_tuan_goods.setAdapter(tuan_list_adapter);
				return;
			} else {
				if (msg.what == Constants.TUAN_INIT_LOAD) {
					ll_tuan_reloading.setVisibility(View.GONE);
					lv_tuan_goods.setVisibility(View.VISIBLE);
					ll_tuan_record.setVisibility(View.VISIBLE);
					ArrayList<TuanMain> list;
					list = (ArrayList<TuanMain>) msg.obj;

					if (maxSize > 0 && list != null) {
						for (int i = 0; i < list.size(); i++)
							tuanList.add(list.get(i));
						ll_tuan_loading.setVisibility(View.VISIBLE);
						tv_tuan_reload.setVisibility(View.GONE);
					} else {
						ll_tuan_loading.setVisibility(View.GONE);
					}
					tv_tuan_total.setText(TuanMainActivity.this.getString(
							R.string.tuan_tv_total).replace("?",
							String.valueOf(maxSize)));
					lv_tuan_goods.setAdapter(tuan_list_adapter);
				} else if (msg.what == Constants.TUAN_LOAD_MORE) {
					ArrayList<TuanMain> list;
					list = (ArrayList<TuanMain>) msg.obj;
					if (list == null || list.size() == 0
							|| tuanList.size() + list.size() >= maxSize + 1) {
						tv_tuan_isLastOne.setVisibility(View.VISIBLE);
						ll_tuan_loading.setVisibility(View.GONE);
					} else {
						for (int i = 0; i < list.size(); i++)
							tuanList.add(list.get(i));
						tv_tuan_isLastOne.setVisibility(View.GONE);
					}
					tuan_list_adapter.notifyDataSetChanged();
				}
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == Constants.TUAN_CITY_SELECTED) {
			if (resultCode == Activity.RESULT_OK) {
				String cityName = data
						.getStringExtra(Constants.TUAN_CITY_CITYNAME);
				tv_tuan_city.setText(cityName);
				tv_tuan_city.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
				spr.OpenEditor();
				spr.putValue(Constants.TUAN_CITYID,
						databaseManager.getCityIdByName(cityName));
				spr.commitEditor();
				spr.closeEditor();
				reloadData();
			}
		}
		if (requestCode == Constants.TUAN_SEARCH_SELECTED) {
			if (resultCode == Activity.RESULT_OK) {
				searchKey = data.getStringExtra(Constants.TUAN_SEARCH_KEY);
				et_tuan_search.setText(searchKey);
				if (preCatId == 0) {
					reloadData();
				}
				sp_tuan_cat.setSelection(0);
			}
		}
	}

	private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			if (!NetworkUtils.isNetworkConnected(context)) {
				Toast.makeText(
						TuanMainActivity.this,
						TuanMainActivity.this
								.getString(R.string.network_disconnect),
						Toast.LENGTH_SHORT).show();
			} else if (maxSize == 0) {
				tv_tuan_isLastOne.setVisibility(View.GONE);
			}
			if (!isFirstIn) {
				tv_tuan_isLastOne.setVisibility(View.GONE);
				tv_tuan_reload.setVisibility(View.VISIBLE);
				ll_tuan_loading.setVisibility(View.GONE);
			}

		}
	};

	public void backToHome(View view) {
		Intent intent = new Intent(TuanMainActivity.this, IndexActivity.class);
		startActivity(intent);
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mNetworkStateReceiver);
	}
}
// spr = new SPResolver(this, Constants.SP_SYSTEM, Context.MODE_PRIVATE);
// initTextViews();
// initBottomLine();
// initViewPager();
// initActionListener();
// IntentFilter filter = new IntentFilter();
// filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
// registerReceiver(mNetworkStateReceiver, filter);
// }
//
// public void initTextViews() {
// tv_tuan_city = (TextView) findViewById(R.id.tv_tuan_city);
//
// String cityName = databaseManager.getCityNameById(spr.getValue(
// Constants.TUAN_CITYID, "3"));
// if (!"".equals(cityName))
// tv_tuan_city.setText(cityName);
//
// tv_tuan_city.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
// tv_tab_food = (TextView) findViewById(R.id.tv_tab_food);
// tv_tab_fun = (TextView) findViewById(R.id.tv_tab_fun);
// tv_tab_makeup = (TextView) findViewById(R.id.tv_tab_makeup);
// tv_tab_buy = (TextView) findViewById(R.id.tv_tab_buy);
// tv_tab_sport = (TextView) findViewById(R.id.tv_tab_sport);
// tv_tab_other = (TextView) findViewById(R.id.tv_tab_other);
// tv_tuan_network = (TextView) findViewById(R.id.tv_tuan_network);
//
// }
//
// private void initBottomLine() {
// iv_bottom_line = (ImageView) findViewById(R.id.iv_bottom_line);
// DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(dm);
// bottomLineWidth = BitmapFactory.decodeResource(getResources(),
// R.drawable.title_bar_mark).getWidth();
// int screenW = dm.widthPixels;
// offset = (float) ((screenW / 6.0 - bottomLineWidth) / 2);
// Matrix matrix = new Matrix();
// matrix.postTranslate(offset, 0);
// iv_bottom_line.setImageMatrix(matrix);
// }
//
// private void initViewPager() {
// vp_tuan_goods = (ViewPager) findViewById(R.id.vp_tuan_goods);
// initFragmentList();
// tfp_adapter = new TuanFragmentPagerAdapter(getSupportFragmentManager(),
// fragmentList);
// vp_tuan_goods.setOffscreenPageLimit(5);
// vp_tuan_goods.setAdapter(tfp_adapter);
// vp_tuan_goods.setCurrentItem(0);
// vp_tuan_goods.setOnPageChangeListener(new TuanOnPageChangeListener());
//
// }
//
// private void initFragmentList() {
// fragmentList = new ArrayList<TuanViewPagerFragment>();
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_FOOD,
// tv_tuan_city.getText().toString()));
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_FUN,
// tv_tuan_city.getText().toString()));
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_MAKEUP,
// tv_tuan_city.getText().toString()));
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_BUY,
// tv_tuan_city.getText().toString()));
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_SPORT,
// tv_tuan_city.getText().toString()));
// fragmentList.add(new TuanViewPagerFragment(Constants.TUAN_CATID_OTHER,
// tv_tuan_city.getText().toString()));
// }
//
// public void initActionListener() {
//
// tv_tuan_city.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// TuanMainActivity.this.startActivityForResult(new Intent(
// TuanMainActivity.this, TuanSelectCityActivity.class),
// CITY);
// overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
// }
//
// });
// tv_tab_food.setOnClickListener(new TuanTabOnClickListener(0));
// tv_tab_fun.setOnClickListener(new TuanTabOnClickListener(1));
// tv_tab_makeup.setOnClickListener(new TuanTabOnClickListener(2));
// tv_tab_buy.setOnClickListener(new TuanTabOnClickListener(3));
// tv_tab_sport.setOnClickListener(new TuanTabOnClickListener(4));
// tv_tab_other.setOnClickListener(new TuanTabOnClickListener(5));
// }
//
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data)
// {
// // TODO Auto-generated method stub
//
// if (requestCode == CITY) {
// if (resultCode == Activity.RESULT_OK) {
// String city = data.getStringExtra(Constants.TUAN_CITY_CITYNAME);
// tv_tuan_city.setText(city);
// tv_tuan_city.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
// spr.OpenEditor();
// spr.putValue(Constants.TUAN_CITYID,
// databaseManager.getCityIdByName(city));
// spr.commitEditor();
// spr.closeEditor();
//
// for (int i = 0; i < fragmentList.size(); i++) {
// fragmentList.get(i).setCityName(city);
// }
// tfp_adapter.notifyDataSetChanged();
// }
// }
// }
//
// public class TuanTabOnClickListener implements View.OnClickListener {
// private int index = 0;
//
// public TuanTabOnClickListener(int index) {
// this.index = index;
// }
//
// @Override
// public void onClick(View v) {
// if (vp_tuan_goods.getCurrentItem() == index) {
// fragmentList.get(index).forceUpdate();
// tfp_adapter.notifyDataSetChanged();
// } else{
// vp_tuan_goods.setCurrentItem(index);
// }
// }
// }
//
// public class TuanOnPageChangeListener implements OnPageChangeListener {
//
// private float base;
// private int currIndex = 0;
//
// public TuanOnPageChangeListener() {
// this.base = offset * 2 + bottomLineWidth;
// }
//
// @Override
// public void onPageSelected(int arg0) {
// animation = new TranslateAnimation(base * currIndex, base * arg0,
// 0, 0);
// currIndex = arg0;
// animation.setFillAfter(true);
// animation.setDuration(300);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// handler.sendEmptyMessage(0);
// }
//
// });
// }
//
// @Override
// public void onPageScrolled(int arg0, float arg1, int arg2) {
// }
//
// @Override
// public void onPageScrollStateChanged(int arg0) {
// }
// }
//
// public void backToHome(View view) {
// cancelThread = true;
// Intent intent = new Intent(TuanMainActivity.this, IndexActivity.class);
// startActivity(intent);
// overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
// }
//
// public void back(View view) {
// cancelThread = true;
// this.finish();
// overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
// }
//
// @Override
// public void onBackPressed() {
// // TODO Auto-generated method stub
// cancelThread = true;
// super.onBackPressed();
// this.finish();
// overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
// }
//
// public Handler handler = new Handler() {
//
// @Override
// public void handleMessage(Message msg) {
// // TODO Auto-generated method stub
// iv_bottom_line.startAnimation(animation);
// }
//
// };
//
// @Override
// protected void onDestroy() {
// // TODO Auto-generated method stub
// cancelThread = true;
// super.onDestroy();
// unregisterReceiver(mNetworkStateReceiver);
//
// }
//
// private BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
//
// @Override
// public void onReceive(Context context, Intent intent) {
// // TODO Auto-generated method stub
// if (!NetworkUtils.isNetworkConnected(context)) {
// vp_tuan_goods.setVisibility(View.INVISIBLE);
// tv_tuan_network.setVisibility(View.VISIBLE);
// Toast.makeText(
// TuanMainActivity.this,
// TuanMainActivity.this
// .getString(R.string.network_disconnect),
// Toast.LENGTH_LONG).show();
// } else {
// vp_tuan_goods.setVisibility(View.VISIBLE);
// tv_tuan_network.setVisibility(View.GONE);
// tfp_adapter.notifyDataSetChanged();
// }
// }
//
// };
